package com.wind.cloud.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * 创建时间、更新时间自动填充(依赖mybatis plus)
 *
 * @author wind.
 */
public class TimeMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        // 填充创建时间、更新时间，类型必需为：LocalDateTime
        Object createdTime = getFieldValByName("createdTime", metaObject);
        if (createdTime == null) {
            setInsertFieldValByName("createdTime", LocalDateTime.now(), metaObject);
        }
        Object updatedTime = getFieldValByName("updatedTime", metaObject);
        if (updatedTime == null) {
            setUpdateFieldValByName("updatedTime", LocalDateTime.now(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 填充更新时间
        setUpdateFieldValByName("updatedTime", LocalDateTime.now(), metaObject);
    }

}
